package Medium.String;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: white-zhou
 * @date: 2024-09-22 19:14
 **/
public class LetterCombinations {
    String[] map = new String[]{
            " ", "*", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"
    };
    public List<String> letterCombinations(String digits) {
        List<String> res = new ArrayList<>();
        if (digits.isEmpty()) {
            return res;
        }
        dfs(digits, 0, "", res);
        return res;
    }

    public void dfs(String digits, int index, String path, List<String> res) {
        if (index == digits.length()) {
            res.add(path);
            return;
        }
        char digit = digits.charAt(index);
        String letters = map[digit - '0'];
        for (int i = 0; i < letters.length(); i++) {
            dfs(digits, index + 1, path + letters.charAt(i), res);
        }
    }
}
